package com.hzw.saas.service.rss.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.rss.IJournalArticleService;
import com.hzw.saas.api.rss.IJournalUserActionService;
import com.hzw.saas.api.rss.IJournalUserAxisService;
import com.hzw.saas.api.rss.bo.JournalArticleBO;
import com.hzw.saas.api.rss.bo.JournalUserActionBO;
import com.hzw.saas.api.rss.bo.JournalUserAxisBO;
import com.hzw.saas.api.rss.query.JournalArticleFavQuery;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.config.util.PageUtils;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.SnowFlakeUtil;
import com.hzw.saas.service.rss.mapper.JournalArticleMapper;
import com.hzw.saas.service.rss.mapper.JournalUserAxisMapper;
import com.hzw.saas.service.rss.model.JournalArticle;
import com.hzw.saas.service.rss.model.JournalUserAxis;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@RequiredArgsConstructor
@Service("journalUserAxisService")
public class JournalUserAxisServiceImpl extends ServiceImpl<JournalUserAxisMapper, JournalUserAxis> implements IJournalUserAxisService, IService<JournalUserAxis> {

    @Autowired
    private JournalUserAxisMapper journalUserAxisMapper;
    @Override
    public void addRecord(String userId, String journalId){
        JournalUserAxis journalUserAxis = new JournalUserAxis();
        journalUserAxis.setPid(SnowFlakeUtil.nextIdStr());
        journalUserAxis.setJournalId(journalId);
        journalUserAxis.setUserId(userId);
        journalUserAxis.setAxisStatus(0);
        this.save(journalUserAxis);
    }

    @Override
    public void updateRecord(List<String> pid,Integer axisType){
        List<JournalUserAxis> list = this.lambdaQuery().in(JournalUserAxis::getPid, pid).list();
        if (CollectionUtil.isNotEmpty(list)){
            list.forEach(journalUserAxis -> {
                journalUserAxis.setAxisEnd(new Date());
                journalUserAxis.setAxisStatus(1);
                journalUserAxis.setAxisType(axisType);
                this.updateById(journalUserAxis);
            });
        }
    }


    @Override
    public List<JournalUserAxisBO> findIfRead(String userId, List<String> journalId){
        //加入时间轴概念，只保留最近一个月的数据
        if (CollectionUtil.isNotEmpty(journalId)){
            List<JournalUserAxis> list = this.lambdaQuery().eq(JournalUserAxis::getUserId, userId)
                .in(JournalUserAxis::getJournalId, journalId)
                .orderByDesc(JournalUserAxis::getCreateTime)
                .list();
            return MapperUtil.nf().mapAsList(list,JournalUserAxisBO.class);
        }
        return Collections.emptyList();
    }

    @Override
    public List<JournalUserAxisBO> findByUserAndJournalId(String userId, String rssId) {
        List<JournalUserAxis> list = this.lambdaQuery().eq(JournalUserAxis::getUserId, userId)
            .eq(JournalUserAxis::getJournalId, rssId)
            .orderByDesc(JournalUserAxis::getCreateTime)
            .list();
        if (CollectionUtil.isNotEmpty(list)){
            return MapperUtil.nf().mapAsList(list,JournalUserAxisBO.class);
        }
        return null;
    }

    @Override
    public void saveBatch(List<JournalUserAxisBO> journalUserAxisBOS) {
        if (CollectionUtil.isNotEmpty(journalUserAxisBOS)){
            List<JournalUserAxis> journalUserAxes = MapperUtil.nf().mapAsList(journalUserAxisBOS, JournalUserAxis.class);
            journalUserAxes.forEach(journalUserAxis -> {
                journalUserAxis.setPid(SnowFlakeUtil.nextIdStr());
                journalUserAxis.setAxisStatus(0);
            });
            this.saveBatch(journalUserAxes);
        }
    }

    @Override
    public List<JournalUserAxisBO> findUnRead(String userId, List<String> collect) {
        if (CollectionUtil.isNotEmpty(collect)){
            List<JournalUserAxis> list = this.lambdaQuery().eq(JournalUserAxis::getUserId, userId)
                .in(JournalUserAxis::getJournalId, collect)
                .isNotNull(JournalUserAxis::getAxisEnd)
                .list();
            return MapperUtil.nf().mapAsList(list,JournalUserAxisBO.class);
        }
        return Collections.emptyList();
    }

    @Override
    public void deleteByUserIdAndPid(String pid, String userId) {
        List<JournalUserAxis> list = this.lambdaQuery().eq(JournalUserAxis::getUserId, userId)
            .eq(JournalUserAxis::getJournalId, pid)
            .list();
        if (CollectionUtil.isNotEmpty(list)){
            journalUserAxisMapper.deleteByPid(list.get(0).getPid());
        };
    }

    @Override
    public void deleteByUserAndJournalIdS(String userId, List<String> pids) {
        if (CollectionUtil.isEmpty(pids)||StrUtil.isBlank(userId)){
            return;
        }
        journalUserAxisMapper.deleteByUserIdAndJournalIds(userId,pids);
    }

}
